const MapValues = require('../template')

console.log("=== MapValues 性能测试 ===")

// 测试小对象映射值性能
console.log(
  "小对象映射值性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValues(obj, value => value * 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试大对象映射值性能
console.log(
  "大对象映射值性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValues(obj, value => value * 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试深度映射值性能
console.log(
  "深度映射值性能:", (() => {
    const obj = { a: { b: { c: 1 } }, d: { e: 2 } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesDeep(obj, value => {
        if (typeof value === 'object' && !Array.isArray(value)) {
          return { ...value, processed: true }
        }
        return value * 2
      }, 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.1 // 平均每次映射时间应该小于0.1ms
  })()
)

// 测试条件映射值性能
console.log(
  "条件映射值性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesWithCondition(obj, value => value * 2, (value, key) => value > 500)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试类型映射值性能
console.log(
  "类型映射值性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i % 2 === 0 ? i : `string${i}`
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesWithType(obj, value => value * 2, 'number')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试键映射值性能
console.log(
  "键映射值性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesWithKey(obj, value => value * 2, (value, key) => key.toUpperCase())
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试正则映射值性能
console.log(
  "正则映射值性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`user_${i}`] = i
      obj[`admin_${i}`] = i
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesWithPattern(obj, value => value.toString().toUpperCase(), /^user_/)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试验证映射值性能
console.log(
  "验证映射值性能:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = { name: { type: 'string' }, age: { type: 'number' }, email: { type: 'string' } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        MapValues.mapValuesWithValidation(obj, value => value, schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试统计映射值性能
console.log(
  "统计映射值性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValuesWithStats(obj, value => value * 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次映射时间应该小于0.01ms
  })()
)

// 测试异步映射值性能
console.log(
  "异步映射值性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(MapValues.mapValuesAsync(obj, value => value * 2, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => typeof result === 'object') &&
        avgTime < 5 // 平均每次映射时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试并发映射值性能
console.log(
  "并发映射值性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(MapValues.mapValuesConcurrent(obj, value => value * 2, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value > 2
      }, 3))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => typeof result === 'object') &&
        avgTime < 10 // 平均每次映射时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试复杂结构映射值性能
console.log(
  "复杂结构映射值性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      MapValues.mapValues(obj, value => {
        if (typeof value === 'object' && !Array.isArray(value)) {
          return { ...value, processed: true }
        }
        if (Array.isArray(value)) {
          return value.map(item => ({ ...item, processed: true }))
        }
        return value
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次映射时间应该小于1ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000
    const results = []

    for (let i = 0; i < iterations; i++) {
      const mapped = MapValues.mapValues(obj, value => value * 2)
      results.push(mapped)
    }

    return results.length === iterations &&
      results.every(result => typeof result === 'object' && result.a === 2 && result.b === 4 && result.c === 6)
  })()
)

console.log("=== MapValues 性能测试完成 ===")
